package com.tcm.pay.util;

import cn.hutool.core.util.ObjectUtil;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String工具类
 *
 * @author sunxy 2016年1月20日 下午4:29:26
 * @since 1.0
 */
public class StringUtils {

    public static String cutIfExceed(String original, int length) {
        if (ObjectUtil.isEmpty(original) || ObjectUtil.isNull(original) || original.length() <= length) {
            return original;
        } else {
            return original.substring(0, length);
        }
    }

    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 该方法主要使用正则表达式来判断字符串中是否包含字母
     *
     * @param cardNum 待检验的原始卡号
     * @return 返回是否包含
     * @author fenggaopan 2015年7月21日 上午9:49:40
     */
    public static boolean isContainsStr(String cardNum) {
        String regex = ".*[a-zA-Z]+.*";
        Matcher m = Pattern.compile(regex).matcher(cardNum);
        return m.matches();
    }

    public static boolean isNotBlank(String target) {
        return (target != null && !"".equals(target));
    }

    public static boolean isBlank(String target) {
        return (target == null || "".equals(target));
    }

    /**
     * 将非空的 String 去空格
     *
     * @param target
     */
    public static String trimString(String target) {
        return isNotBlank(target) ? target.trim() : null;
    }

    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isDouble(String value) {
        try {
            Double.valueOf(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }


    public static boolean contains(String str, String searchStr) {
        if (str != null && searchStr != null) {
            return str.indexOf(searchStr) >= 0;
        } else {
            return false;
        }
    }


    public static boolean isCorresponding(String strName, String str) {
        String reg = ".*" + str + ".*";
        return strName.matches(reg);
    }

    public static String toScaleSix(Double d) {
        DecimalFormat formater = new DecimalFormat("#0.######");
        formater.setRoundingMode(RoundingMode.FLOOR);
        return formater.format(d);
    }

    public static boolean matchRegular(String str, String reg) {
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    //去空
    public static String replaceNull(String str) {
        if (isNotBlank(str)) {
            return str.replace(" ", "");
        }
        return str;
    }

    //截取字符窜长度
    public static String toSubstring(String str, int i) {
        if(StringUtils.isBlank(str)){
            return str;
        }
        if (str.length() <= i) {
            return str;
        }
        return str.substring(0, i);
    }

    public static List<String> string2List(String value) {
        String[] split = value.split(",");
        List<String> list = new ArrayList<String>();
        for (String s : split) {
            list.add(s);
        }
        return list;
    }

}
